<template>
  <component
    :is="tag"
    :to="to"
    :class="[
      'logo-title',
      {
        'logo-title--clickable': to || onClick,
        'logo-title--small': size === 'small',
        'logo-title--large': size === 'large'
      }
    ]"
    @click="handleClick"
  >
    <img
      v-if="showLogo"
      :src="logoSrc"
      :alt="logoAlt"
      class="logo-title__icon"
    />
    <span class="logo-title__text">{{ title }}</span>
  </component>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import logoImage from '@/assets/images/logo.png'

export interface LogoTitleProps {
  /** 标题文本 */
  title?: string
  /** 是否显示Logo图标 */
  showLogo?: boolean
  /** Logo图片路径，默认使用内置logo */
  logoSrc?: string
  /** Logo图片alt文本 */
  logoAlt?: string
  /** 组件大小 */
  size?: 'small' | 'medium' | 'large'
  /** 路由跳转地址，如果提供则渲染为router-link */
  to?: string
  /** 点击事件回调 */
  onClick?: () => void
}

const props = withDefaults(defineProps<LogoTitleProps>(), {
  title: '懒熊文档',
  showLogo: true,
  logoSrc: logoImage,
  logoAlt: 'Logo',
  size: 'medium'
})

const emit = defineEmits<{
  click: []
}>()

// 使用路由
const router = useRouter()

// 根据是否有to属性决定渲染的标签
const tag = computed(() => {
  return props.to ? 'router-link' : 'div'
})

const handleClick = () => {
  if (props.onClick) {
    props.onClick()
  }
  emit('click')
  
  // 直接跳转到应用列表页面
  router.push('/apps')
}
</script>

<style scoped>
.logo-title {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  font-weight: 600;
  transition: all 0.2s ease;
  user-select: none;
  text-decoration: none;
  color: var(--el-text-color-primary);
  cursor: pointer;
}

.logo-title--clickable {
  cursor: pointer;
}

.logo-title--clickable:hover {
  color: var(--el-color-primary);
  text-decoration: none;
}

.logo-title__icon {
  width: auto;
  height: 80px;
  object-fit: contain;
  flex-shrink: 0;
}

.logo-title__text {
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.2;
  color: var(--el-text-color-primary);
  margin-top: 3px;
}

/* 不同尺寸样式 */
.logo-title--small .logo-title__icon {
  height: 2rem;
}

.logo-title--small .logo-title__text {
  font-size: 1rem;
}

.logo-title--large .logo-title__icon {
  height: 3.5rem;
}

.logo-title--large .logo-title__text {
  font-size: 1.5rem;
}

/* 深色模式适配 - Element Plus CSS变量会自动适配，无需额外设置 */

/* 响应式设计 */
@media (max-width: 768px) {
  .logo-title__icon {
    height: 2.2rem;
  }

  .logo-title__text {
    font-size: 1.1rem;
  }

  .logo-title--large .logo-title__icon {
    height: 3rem;
  }

  .logo-title--large .logo-title__text {
    font-size: 1.3rem;
  }
}

@media (max-width: 480px) {
  .logo-title {
    gap: 0.3rem;
  }

  .logo-title__icon {
    height: 2rem;
  }

  .logo-title__text {
    font-size: 1rem;
  }

  .logo-title--large .logo-title__icon {
    height: 2.5rem;
  }

  .logo-title--large .logo-title__text {
    font-size: 1.2rem;
  }
}
</style>